package MyApp.View.wellGroup
{
	import MyApp.Model.Well;
	import MyApp.Model.WellGroup;
	import MyApp.Model.proxy.WellGroupProxy;
	import MyApp.MyAppFacade;
	import MyApp.View.CommonMediator;
	
	import components.wellGroup.AddWell;
	import components.wellGroup.AddWellGroup;
	import components.wellGroup.WellGroupWindow;
	
	import flash.events.MouseEvent;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.events.FlexEvent;
	
	import org.puremvc.as3.interfaces.INotification;
	
	import util.Event.ValueEvent;

	public class WellGroupMediator extends CommonMediator
	{
		public static const NAME:String="WellGroupMediator";
		public function WellGroupMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
			this.CommonRegister(view,view.well_group_list);
			view.addEventListener(WellGroupWindow.DO_ADD_WELL_GROUP,doAddWellGroup);
			view.addEventListener(WellGroupWindow.DO_ADD_WELL,doAddWell);
			view.addEventListener(WellGroupWindow.DO_UPDATE,doUpdate);
			view.addEventListener(WellGroupWindow.DO_EDIT_BACK,doEditBack);
			view.addEventListener(WellGroupWindow.DO_SAVE_WELL_GROUP,doSaveWellGroup);
			view.addEventListener(WellGroupWindow.DO_UNBIND,doUnBind);
			view.addEventListener(WellGroupWindow.DO_BIND,doBind);
			view.addEventListener(WellGroupWindow.DO_DELETE,doDelete);
			view.addEventListener(WellGroupWindow.DO_DELETE_WELL,doDeleteWell);
		}
		private function get view():WellGroupWindow{
			return viewComponent as WellGroupWindow;
		}
		protected function doDeleteWell(e:ValueEvent):void{
			var wg:WellGroup=view.editView.wg;
			var w:Well=e.vo as Well;
			this.sendNotification(WellGroupWindow.DO_DELETE_WELL,{wellId:w.id,wellGroupId:wg.wellGroupId});
		}
		protected function doDelete(e:ValueEvent):void{
			var wg:WellGroup=e.vo as WellGroup;
			this.sendNotification(WellGroupWindow.DO_DELETE,{wellGroupId:wg.wellGroupId,pager:view.well_group_list.mPager});
		}
		protected function doBind(e:ValueEvent):void{
			var wg:WellGroup=view.editView.wg;
			var w:Well=e.vo as Well;
			this.sendNotification(WellGroupWindow.DO_BIND,{wellId:w.id,wellGroupId:wg.wellGroupId});
		}
		protected function doUnBind(e:ValueEvent):void{
			var wg:WellGroup=view.editView.wg;
			var w:Well=e.vo as Well;
			this.sendNotification(WellGroupWindow.DO_UNBIND,{wellId:w.id,wellGroupId:wg.wellGroupId});
		}
		protected function doSaveWellGroup(e:MouseEvent):void{
			view.editView.currentState="loading";
			this.sendNotification(WellGroupWindow.DO_SAVE_WELL_GROUP,view.editView.wg);
		}
		protected function doEditBack(e:MouseEvent):void{
			view.currentState="basic";
			this.sendNotification(WellGroupWindow.DO_REFRESH,view.well_group_list.mPager);
		}
		protected function doUpdate(e:ValueEvent):void{
			view.currentState="detail";
			var wg:WellGroup=e.vo as WellGroup;
			view.editView.wg=wg;
			view.editView.initInfo();
			this.sendNotification(WellGroupWindow.DO_REFRESH_WELL,{wellGroupId:wg.wellGroupId});
		}
		protected function doAddWell(e:ValueEvent):void{
			var window:AddWell=AddWell(MyAppFacade.openPopUpWindow(AddWell,AddWellMediator,AddWellMediator.NAME));
			window.wg=e.vo as WellGroup;
			window.pager=view.well_group_list.mPager;
		}
		protected function doAddWellGroup(e:MouseEvent):void{
			MyAppFacade.openPopUpWindow(AddWellGroup,AddWellGroupMediator,AddWellGroupMediator.NAME);
		}
		override public function listNotificationInterests():Array{
			return [WellGroupProxy.DATA_LOAD_OK
			,WellGroupProxy.LOAD_WELL_OK
			,WellGroupProxy.UPDATE_WG_OK];
		}
		override public function handleNotification(notification:INotification):void{
			switch(notification.getName()){
				case WellGroupProxy.DATA_LOAD_OK:
					view.well_group_list.dataProvider=notification.getBody().list;
					view.well_group_list.mPager=notification.getBody().pager;
					break;
				case WellGroupProxy.LOAD_WELL_OK:
					var obj:Object=notification.getBody();
					view.editView.bindList=obj.bindList as ArrayCollection;
					view.editView.unbindList=obj.unbindList as ArrayCollection;
					break;
				case WellGroupProxy.UPDATE_WG_OK:
					view.editView.currentState="basic";
					break;
			}
		}
	}
}